home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap2 / 2_3 / testprot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  2.4 KB  |  111 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* function prototype */
  6. int getvalue(char *s, char** name, char** version);
  7.  
  8. main()
  9. {
  10.     char* server_protocol = getenv("SERVER_PROTOCOL");
  11.     char *name, *versionStr;
  12.     float versionNum;
  13.  
  14.     /* output html MIME type */
  15.     printf("Content-type: text/html\n\n");
  16.  
  17.     printf("<HTML>\n");
  18.     printf("<HEAD><TITLE>CGI Script How-to: Test Script</TITLE></HEAD>\n");
  19.     printf("<BODY>\n");
  20.  
  21.     printf("<H1>CGI Script How-to determine the protocol being used by the server</H1>\n");
  22.  
  23.     /* If name/version strings have been extracted then getvalue returns 0
  24.      * otherwise the value is NULL or in the wrong format.
  25.      */
  26.  
  27.     if (getvalue(server_protocol, &name, &versionStr) == 0)
  28.     {
  29.         /* Use the HTTP information here and convert
  30.          * the revision string to a floating-point number
  31.          */
  32.         versionNum = atof(versionStr);
  33.  
  34.         /* Test the version number: greater, equal, or less than 1.0 */
  35.  
  36.         if (versionNum > 1.0)
  37.         {
  38.             printf("Your server is using a new HTTP protocol\n");
  39.         }
  40.         else if (versionNum == 1.0)
  41.         {
  42.             printf("Your server is using the current HTTP protocol\n");
  43.         }
  44.         else if (versionNum > 0.0)
  45.         {
  46.             printf("Your server is using the old HTTP protocol\n");
  47.         }
  48.         else
  49.         {
  50.             /* version is zero or not even a number */
  51.             printf("Server protocol %s/%s is unknown\n", name, versionStr);
  52.         }
  53.     }
  54.     else
  55.     {
  56.         /* value is NULL or stored in a non-standard format */
  57.         printf("Server protocol is unknown\n");
  58.     }
  59.  
  60.     printf("</BODY></HTML>\n\n");
  61.     exit(0);
  62. }
  63.  
  64.  
  65. /*
  66.  * function getvalue()
  67.  *
  68.  * Parses an input string (s) in the form name/version, extracts both the name
  69.  * and version elements and stores these in two output string variables (name,
  70.  * version).
  71.  *
  72.  *    Returns:  0 if name/version values extracted from target string,
  73.  *             -1 if values not present
  74.  */
  75.  
  76. int getvalue(char *s, char** name, char** version) 
  77. {
  78.     char *p;
  79.     if (s == NULL || *s == '/')
  80.     {
  81.         return -1;        /* null string or no name field */
  82.     }
  83.  
  84.     p = strchr(s, '/');        /* Locate the slash (/) in the string */
  85.     if (p == 0)
  86.     {
  87.         return -1;            /* '/' character not found */
  88.     }
  89.  
  90.     *name = malloc(p-s+1);
  91.     if (*name == NULL)
  92.     {
  93.         return -1;            /* malloc failed */
  94.     }
  95.     strncpy(*name, s, p-s);
  96.     (*name)[p-s] = '\0';            /* terminate the string */
  97.  
  98.     *version = malloc(strlen(p));
  99.     if (*version == NULL)
  100.     {
  101.         return -1;            /* malloc failed */
  102.     }
  103.     strcpy(*version, p+1);
  104.  
  105.     return 0;                /* okay, value set */
  106. }
  107.  
  108. /*
  109.  * end of testprot.c
  110.  */
  111.